package tests;

import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class PublishPage extends Utils {
    private static String url = "http://8.134.99.135:58080/index.html";

    public PublishPage() {
        super(url);
    }

    //未登录的状态下，自动跳回登录页面
    public void noLogin(){
        String currentUrl = driver.getCurrentUrl();
        Assertions.assertEquals("http://8.134.99.135:58080/sign-in.html",currentUrl);
    }

    //登录状态下
    public void loginSuc(){
        //检查页面是否可以正确加载
        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > " +
                "div.col-auto.ms-auto.d-print-none > div")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#bit-forum-content > div.page-body " +
                "> div > div > div.card-body")));
        //检查编辑发布的页面是否正确加载
        String title = driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > " +
                "div > div > h2")).getText();
        Assertions.assertEquals("发新贴",title);

        String classification = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div " +
                "> div.card-body > div:nth-child(1) > label > strong")).getText();
        Assertions.assertEquals("版块",classification);
        driver.findElement(By.cssSelector("#article_post_borad"));

        String headline = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div.card-body " +
                "> div:nth-child(2) > label > strong")).getText();
        Assertions.assertEquals("标题",headline);
        driver.findElement(By.cssSelector("#article_post_title"));

        String content = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > " +
                "div.card-body > div:nth-child(3) > label > strong")).getText();
        Assertions.assertEquals("内容",content);

        String push = driver.findElement(By.cssSelector("#article_post_submit")).getText();
        Assertions.assertEquals("发 布",push);

    }

    //缺项发布
    public void noPush(){
        //发布按钮
        WebElement pushElement = driver.findElement(By.cssSelector("#article_post_submit"));
        Actions action = new Actions(driver);


        //1、缺少内容
//        driver.findElement(By.cssSelector("#article_post_title")).sendKeys("测试发布帖子");
//        action.moveToElement(pushElement).click().perform();
//        String content = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
//        String[] s2 = content.split("\n");
//        Assertions.assertEquals("请输入帖子内容",s2[2]);
//        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div >" +
//                " span.close-jq-toast-single")).click();
//        driver.findElement(By.cssSelector("#article_post_title")).clear();

        //2、都缺少
//        WebElement allNoElement = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div"));
//        wait.until(ExpectedConditions.elementToBeClickable(allNoElement));
//        action.moveToElement(pushElement).click().perform();
//        String errorText = allNoElement.getText();
//        String[] s = errorText.split("\n");
//        Assertions.assertEquals("请输入帖子标题",s[2]);
//        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > " +
//                "span.close-jq-toast-single")).click();


        //3、缺少标题
//        driver.findElement(By.cssSelector("#article_post_title")).clear();
        driver.findElement(By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > " +
                "li:nth-child(7) > a > i")).click();
        action.moveToElement(pushElement).click().perform();
        String title = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        String[] s3 = title.split("\n");
        Assertions.assertEquals("请输入帖子标题",s3[2]);
        driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > " +
                "span.close-jq-toast-single")).click();
    }

    //成功发布
    public void pushSuc() throws InterruptedException {
        WebElement pushElement = driver.findElement(By.cssSelector("#article_post_submit"));

        driver.findElement(By.cssSelector("#edit-article > div.editormd-toolbar > div > ul > " +
                "li:nth-child(7) > a > i")).click();
        driver.findElement(By.cssSelector("#article_post_title")).sendKeys("测试发布帖子");
        Thread.sleep(1000);
        Actions action = new Actions(driver);
        action.moveToElement(pushElement).click().perform();
        String text = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        String[] s = text.split("\n");
        Assertions.assertEquals("发帖成功",s[2]);

        //成功发布后，检查是否跳转回首页
        String homePage = driver.findElement(By.cssSelector("#article_list_board_title")).getText();
        Assertions.assertEquals("首页",homePage);
    }
}
